import CryptoJS from 'crypto-js';


const aes_key = "TKVFxXmsEx+Cylo/7RPMV+ixt/VfuMtijpDWJA5yDE4="


/**
 * 加密（需要先加载aes.js文件）
 * @param word
 * @param key 非必填，aes 秘钥
 * @returns {*}
 */
const aesEncrypt = (word, key) => {
	let _key = key == undefined ? CryptoJS.enc.Utf8.parse(aes_key) : CryptoJS.enc.Utf8.parse(key);
	let srcs = CryptoJS.enc.Utf8.parse(word);
	let encrypted = CryptoJS.AES.encrypt(srcs, _key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.toString();
}

/**
 * 解密
 * @param word
 ** @param key 非必填，aes 秘钥
 * @returns {*}
 */
const aesDecrypt = (word, key) => {
	let _key = key == undefined ? CryptoJS.enc.Utf8.parse(aes_key) : CryptoJS.enc.Utf8.parse(key);
	let decrypt = CryptoJS.AES.decrypt(word, _key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

const aesDecryptHex = (word, key) => {
	let keys = CryptoJS.enc.Utf8.parse(key);

	let base64 = CryptoJS.enc.Hex.parse(word);

	let src = CryptoJS.enc.Base64.stringify(base64);

	// 解密模式为CBC，补码方式为PKCS5Padding（也就是PKCS7）
	let decrypt = CryptoJS.AES.decrypt(src, keys, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});

	let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
	return decryptedStr.toString();
}

//md52次加密
const getPasswordMD5 = (pw, pwd_extend) => {
	let pwMd5 = '';
	pwMd5 = String(CryptoJS.MD5(String(CryptoJS.MD5(pw)) + pwd_extend));
	return pwMd5;
}

//md5加密
const getMD5 = (pw, pwd_extend) => {
	let pwMd5 = '';
	if (pwd_extend) {
		pw += pwd_extend
	}
	pwMd5 = String(CryptoJS.MD5(pw));
	return pwMd5;
}



export {
	aesEncrypt,
	aesDecrypt,
	aesDecryptHex,
	getPasswordMD5,
	getMD5
}